<?php

namespace App\Repositories;


use App\Exceptions\Interfaces\CommentServiceException;
use App\Interfaces\CommentService;
use App\Models\Comment;
use App\Models\CommentPicture;
use App\Models\OrderGoods;
use App\Models\Order;
use App\Models\User;

class CommentRepository implements CommentService{

    /**
     * 添加评论
     *
     * @param $user_id
     * @param $order_goods_id
     * @param $star
     * @param $content
     * @param user_id :用户id
     * @throws CommentServiceException
     * @return \App\Models\Comment|bool
     */
    public function createComment($user_id, $order_goods_id,  $star, $content, $label=null,$type,$picture){
        $orderGoods = OrderGoods::find($order_goods_id);
        if ($user_id==''){
            throw new CommentServiceException('缺少user_id参数');
        }
        if($order_goods_id == ""){
            throw new CommentServiceException('缺少order_goods_id参数');
        }
        if($star == ""){
            throw new CommentServiceException('缺少star参数');
        }
//        if($content == ""){
//            throw new CommentServiceException('缺少content参数');
//        }
//        if($label == ""){
//            throw new CommentServiceException('缺少label参数');
//        }
        $comment = new Comment;
        $comment->user_id = $user_id;
        $comment->order_goods_id = $order_goods_id;
        $comment->star = $star;
        $comment->content = $content;
        $comment->label = $label;
        $comment->add_time = date('Y-m-d H:i:s',time());
        $comment->type = $type;
        $comment->picture = $picture;
        $comment->save();
        $orderGoods->comment_status=1;
        $orderGoods->save();

        return $comment->id;

    }//end func createComment()



    public function getComment($goods_id,$condition,$page)
    {
        if ($goods_id==''){
            throw new CommentServiceException('缺少goods_id参数');
        }

        switch ($condition) {
            case 'allComment':
                $comment = Comment::select('comment.*', 'order_goods.goods_id','user.phone')
                    ->join('order_goods', 'comment.order_goods_id', '=', 'order_goods.id')
                    ->join('order','order.order_id','=','order_goods.order_id')
                    ->join('user','comment.user_id','=','user.id')
                    ->where('order_goods.goods_id', $goods_id)
                    ->where('order.order_status','=',4)
                    ->where('order_goods.comment_status','=',1)
                    ->orderBy('comment.add_time','desc')
                    ->paginate(10,['*'],'page',$page);
               // dd($comment);
                break;
            case 'goodComment':
                $comment = Comment::select('comment.*', 'order_goods.goods_id','user.phone')
                    ->join('order_goods', 'comment.order_goods_id', '=', 'order_goods.id')
                    ->join('order','order.order_id','=','order_goods.order_id')
                    ->join('user','comment.user_id','=','user.id')
                    ->where('order_goods.goods_id', $goods_id)
                    ->where('order.order_status','=',4)
                    ->where('order_goods.comment_status','=',1)
                    ->where('star', '>', 3)
                    ->orderBy('comment.add_time','desc')
                    ->paginate(10,['*'],'page',$page);
                break;
            case 'middleComment':
                $comment = Comment::select('comment.*', 'order_goods.goods_id','user.phone')
                    ->join('order_goods', 'comment.order_goods_id', '=', 'order_goods.id')
                    ->join('order','order.order_id','=','order_goods.order_id')
                    ->join('user','comment.user_id','=','user.id')
                    ->where('order_goods.goods_id', $goods_id)
                    ->where('order.order_status','=',4)
                    ->where('order_goods.comment_status','=',1)
                    ->where('star', '=', 3)
                    ->orderBy('comment.add_time','desc')
                    ->paginate(10,['*'],'page',$page);
                break;
            case 'badComment':
                $comment = Comment::select('comment.*', 'order_goods.goods_id','user.phone')
                    ->join('order_goods', 'comment.order_goods_id', '=', 'order_goods.id')
                    ->join('order','order.order_id','=','order_goods.order_id')
                    ->join('user','comment.user_id','=','user.id')
                    ->where('order_goods.goods_id', $goods_id)
                    ->where('order.order_status','=',4)
                    ->where('order_goods.comment_status','=',1)
                    ->where('star', '<', 3)
                    ->orderBy('comment.add_time','desc')
                    ->paginate(10,['*'],'page',$page);
                break;
            default :
                $comment = Comment::select('comment.*','order_goods.goods_id','user.phone')
                ->join('order_goods','comment.order_goods_id','=','order_goods.id')
                ->join('order','order.order_id','=','order_goods.order_id')
                ->join('user','comment.user_id','=','user.id')
                ->where('order_goods.goods_id',$goods_id)
                ->where('order.order_status','=',4)
                ->where('order_goods.comment_status','=',1)
                ->orderBy('comment.add_time','desc')
                ->paginate(10,['*'],'page',$page);
        }
        //dd($comment);
        return $comment;

    }

    /**
     * @param $goods_id 商品ID
     * @throws CommentServiceException
     * @return mixed
     */
    public function countComment($goods_id){
        if ($goods_id==''){
            throw new CommentServiceException('缺少goods_id参数');
        }
        $countComment = Comment::select('comment.*','order_goods.goods_id' )
                                ->join('order_goods','comment.order_goods_id','=','order_goods.id')
                                ->join('order','order.order_id','=','order_goods.order_id')
                                ->where('order_goods.goods_id',$goods_id)
                                ->where('order.order_status','=',4)
                                ->where('order_goods.comment_status','=',1)
                                ->count();
        //dd($countComment);
        return $countComment;

    }//end func countComent

    public function countGoodComment($goods_id){
        if ($goods_id==''){
            throw new CommentServiceException('缺少goods_id参数');
        }
        $countGoodComment = Comment::select('comment.*','order_goods.goods_id' )
                                    ->join('order_goods','comment.order_goods_id','=','order_goods.id')
                                    ->join('order','order.order_id','=','order_goods.order_id')
                                    ->where('order_goods.goods_id',$goods_id)
                                    ->where('order.order_status','=',4)
                                    ->where('order_goods.comment_status','=',1)
                                    ->where('star','>',3)
                                    ->count();

        return $countGoodComment;

    }//end func countGoodComment

    public function countMiddleComment($goods_id){
        if ($goods_id==''){
            throw new CommentServiceException('缺少goods_id参数');
        }
        $countMiddleComment = Comment::select('comment.*','order_goods.goods_id' )
                                    ->join('order_goods','comment.order_goods_id','=','order_goods.id')
                                    ->join('order','order.order_id','=','order_goods.order_id')
                                    ->where('order_goods.goods_id',$goods_id)
                                    ->where('order.order_status','=',4)
                                    ->where('order_goods.comment_status','=',1)
                                    ->where('star','=',3)
                                    ->count();
        return $countMiddleComment;

    }//end func countBadComment

    public function countBadComment($goods_id){
        if ($goods_id==''){
            throw new CommentServiceException('缺少goods_id参数');
        }
        $countBadComment = Comment::select('comment.*','order_goods.goods_id' )
                                ->join('order_goods','comment.order_goods_id','=','order_goods.id')
                                ->join('order','order.order_id','=','order_goods.order_id')
                                ->where('order_goods.goods_id',$goods_id)
                                ->where('order.order_status','=',4)
                                ->where('order_goods.comment_status','=',1)
                                ->where('star','<',3)
                                ->count();
        return $countBadComment;

    }//end func countBadComment

    function getMyOrderGoods($user_id){
        if ($user_id==''){
            throw new CommentServiceException('缺少user_id参数');
        }
        return $myOrderGoods = OrderGoods::select('order_goods.*','order.order_status','order.user_id','order.add_time')
            ->join('order','order.order_id','=','order_goods.order_id')
            ->where('order.user_id',$user_id)
            ->where('order_goods.comment_status','=',0)
            ->where('order.order_status','=',4)
            ->paginate(10);
    }

    function countMyOrderGoods($user_id){
        if ($user_id==''){
            throw new CommentServiceException('缺少user_id参数');
        }
        return $myOrderGoods = OrderGoods::select('order_goods.*','order.order_status','order.user_id','order.add_time')
            ->join('order','order.order_id','=','order_goods.order_id')
            ->where('order.user_id',$user_id)
            ->where('order_goods.comment_status','=',0)
            ->where('order.order_status','=',4)
            ->count();
    }

    function getCommentUser($user_id){
        return $user = User::find($user_id);
    }

    function getCommentPicture($goods_id){
        $commentPictures = CommentPicture::where('goods_id',$goods_id)->get()->toArray();

        return $commentPictures;
    }

}//end class CommentRepository